package com.magic.gre.mvp.presenter;

import com.google.gson.reflect.TypeToken;
import com.magic.gre.entity.UserBean;
import com.magic.gre.entity.base.BaseObjectModel;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.mvp.contract.CodeContract;
import com.magic.gre.mvp.model.CodeModelImpl;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.mvp.BasePresenterImpl;
import com.noname.lib_base_java.net.RxBus;

import java.util.Map;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 15:51
 * 文件名: CodePresenterImpl
 * 描述:
 */
public class CodePresenterImpl extends BasePresenterImpl<CodeContract.View, CodeContract.Model>
        implements CodeContract.Presenter {
    public CodePresenterImpl(CodeContract.View view) {
        super(view);
    }

    @Override
    protected CodeContract.Model creatModel() {
        return new CodeModelImpl();
    }

    @Override
    public void pLogin(Map<String, Object> parmsMap) {
        mModel.mLogin(new CommonObserver<BaseObjectModel<UserBean>>(new TypeToken<BaseObjectModel<UserBean>>() {
        }.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<UserBean> baseObjectModel) {
                mView.hideLoadDialog();
                Apphelper.putToken(baseObjectModel.body.getToken());
                Apphelper.putUserInfo(baseObjectModel.body);
                RxBus.getInstance().post(new MsgEvent(MsgEvent.LOGIN_REGISTER));
                mView.vLogin(baseObjectModel.body);
            }

            @Override
            public void onError(int status, String msg) {
                mView.hideLoadDialog();
                mView.doPrompt(msg);
            }
        }, parmsMap);
    }

    @Override
    public void pRegister(Map<String, Object> parmsMap) {
        mModel.mRegister(new CommonObserver<BaseObjectModel<UserBean>>(new TypeToken<BaseObjectModel<UserBean>>() {
        }.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<UserBean> baseObjectModel) {
                mView.hideLoadDialog();
                Apphelper.putToken(baseObjectModel.body.getToken());
                Apphelper.putUserInfo(baseObjectModel.body);
                RxBus.getInstance().post(new MsgEvent(MsgEvent.LOGIN_REGISTER));
                mView.vRegister(baseObjectModel.body);
            }

            @Override
            public void onError(int status, String msg) {
                mView.hideLoadDialog();
                mView.doPrompt(msg);
            }
        }, parmsMap);
    }

    @Override
    public void pBindPhone(Map<String, Object> parmsMap) {
        mModel.mBindPhone(new CommonObserver<BaseObjectModel<UserBean>>(new TypeToken<BaseObjectModel<UserBean>>(){}.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<UserBean> userBeanBaseObjectModel) {
                mView.hideLoadDialog();
                Apphelper.putToken(userBeanBaseObjectModel.body.getToken());
                Apphelper.putUserInfo(userBeanBaseObjectModel.body);
                RxBus.getInstance().post(new MsgEvent(MsgEvent.LOGIN_REGISTER));
                mView.vBindPhone(userBeanBaseObjectModel.body);
            }

            @Override
            public void onError(int status, String msg) {
                mView.doPrompt(msg);
            }
        }, parmsMap);
    }

    @Override
    public void pSmsCode(String phone,int type) {
        mModel.mSmsCode(new CommonObserver<BaseObjectModel<String>>(new TypeToken<BaseObjectModel<String>>(){}.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<String> stringBaseObjectModel) {
                mView.vSmsCode(stringBaseObjectModel.body);

            }

            @Override
            public void onError(int status, String msg) {
                mView.doPrompt(msg);
                mView.hideLoadDialog();
            }
        },phone,type);
    }
}
